/**
 * @desc 	BUI公共方法类
 *
 * @author 	Damen
 * @date 	2017-09-01
 *
 * @constructor
 */
function BUIClass() {
    this.config = {
        DEFAULT_MSG : "操作成功",
        DEFAULT_TYPE : "info",
        DELAY_TIME : 1000
    };

    /**
     * 注册表单
     * @param formId 表单ID
     */
    this.formRender = function(formId){
        if(formId){
            formId = formId.replace("#",'');
        }
        var form;
        BUI.use('bui/form',function(Form) {
            form = new Form.Form({
                srcNode: "#"+formId
            }).render();
        });
        return form;
    };

    /**
     * 弹出对话框
     * @param dialogParams
     * @returns {*}
     */
    this.overlay = function(dialogParams){
        var dialog;
        BUI.use(['bui/overlay'], function (Overlay) {
            dialog = new Overlay.Dialog(dialogParams);
        });
        return dialog;
    };

    /**
     * 时间控件
     * @param dateParams
     * @returns {*}
     */
    this.calendar =  function (dateParams) {
        var datepicker;
        BUI.use('bui/calendar',function(Calendar){
            datepicker = new Calendar.DatePicker(dateParams);
        });
        return datepicker;
    };

    /**
     * treelist
     * @param treeParams
     * @param callback
     */
    this.tree = function(treeParams,callback) {
        BUI.use('bui/tree',function (Tree) {
            var tree = new Tree.TreeList(treeParams);
            tree.render();
            if($.isFunction(callback)){
                callback(tree);
            }
        });
    };

    /**
     * tab
     * @param tabParams
     * @param callback
     */
    this.tab = function(tabParams,callback){
        BUI.use(['bui/tab','bui/mask'],function(Tab){
            var tab = new Tab.TabPanel(tabParams);
            tab.render();
            if($.isFunction(callback)){
                callback(tab);
            }
        });
    };
    /**
     *  store
     * @param storeParams
     * @param callback
     */
    this.store = function(storeParams,callback){
        var Store = BUI.Data.Store;
        var store = new Store(storeParams);
        if($.isFunction(callback)){
            callback(store);
        }
        return store;
    };
    /**
     * grid
     * @param gridParams
     * @param callback
     */
    this.grid = function(gridParams,callback){
        var Grid = BUI.Grid;
        var grid = new Grid.Grid(gridParams);
        grid.render();
        if($.isFunction(callback)){
            callback(grid);
        }
        return grid;
    };

    /**
     * store
     * @param storeParams
     * @param callback
     */
    this.treeStore = function(storeParams,callback){
        BUI.use(['bui/tree','bui/data'],function (Tree,Data) {
            var store = new Data.TreeStore(storeParams);
            if($.isFunction(callback)){
                callback(store);
            }
        });
    };

    /**
     * tree-grid
     * @param treeParams
     * @param callback
     */
    this.treegrid =  function (treeParams,callback) {
        BUI.use(['bui/extensions/treegrid'],function (TreeGrid) {
            var treegrid = new TreeGrid(treeParams);
            treegrid.render();
            if($.isFunction(callback)){
                callback(treegrid);
            }
        });
    };

    /**
     * 获取子节点信息
     * @param treeNode  当前节点
     * @param treeList  节点信息集合
     * @param id
     * @param text
     * @param pId
     * @param childArr  子节点集合
     * @returns {*}
     */
    this.getTreeChildren = function(treeNode,treeList,id,text,pId,childArr){
        var me = this;
        for(var i=0;i<treeList.length;i++){
            var childNode = treeList[i];
            if(eval("childNode."+pId)==eval("treeNode."+id)){
                treeList.splice(i,1);
                i--;
                var subArr = me.getTreeChildren(childNode,treeList,id,text,pId,new Array());
                childNode.text=eval("childNode."+text);
                childNode.children=subArr;
                childArr.push(childNode);
            }
        }
        return childArr;
    };
    /**
     * 获取grid选择数据
     * @param grid
     * @param rowNum    指定选中条数
     * @param callback
     * @returns {*|Array|Selection}
     */
    this.getGridSelection = function(grid,rowNum,callback){
        var objs = grid.getSelection();
        if (!objs || objs.length == 0) {
            return Message.show('请选择一条记录', 'warning');
        }else if(rowNum&&rowNum>0&&rowNum!=objs.length){
            return Message.show('只能选'+rowNum+'条记录', 'warning');
        } else {
            if($.isFunction(callback)){
                callback(objs);
            }
            return objs;
        }
    }
}